<?php
class Acl extends Zend_Acl {
    public function  __construct() {

        //Добавляем роли
        $this->addRole('guest');
        $this->addRole('user', 'guest');
        $this->addRole('admin', 'user');
        $this->addRole('demo_admin', 'admin');
		
        //Добавляем ресурсы
        // РЕСУРСЫ ГОСТЯ 
		$this->add(new Zend_Acl_Resource('catalog/index/index'));
		$this->add(new Zend_Acl_Resource('catalog/menu/main'));
		$this->add(new Zend_Acl_Resource('catalog/menu/contact'));
		$this->add(new Zend_Acl_Resource('catalog/item/display/:id/:page'));
		$this->add(new Zend_Acl_Resource('catalog/item/success'));
		$this->add(new Zend_Acl_Resource('catalog/index/search'));
		$this->add(new Zend_Acl_Resource('registration/index/index'));
		$this->add(new Zend_Acl_Resource('registration/index/validate'));
		$this->add(new Zend_Acl_Resource('users/user.login/login'));
		$this->add(new Zend_Acl_Resource('catalog/item/display'));
        $this->add(new Zend_Acl_Resource('catalog/menu/tests'));

        //...

        // РЕСУРСЫ ПОЛЬЗОВАТЕЛЯ !
        $this->add(new Zend_Acl_Resource('users/user-login/profile'));
        $this->add(new Zend_Acl_Resource('index/index'));
        $this->add(new Zend_Acl_Resource('users/index/index'));
        $this->add(new Zend_Acl_Resource('users/user-login/userprofile'));
      	$this->add(new Zend_Acl_Resource('users/user-login/editprofile'));
        $this->add(new Zend_Acl_Resource('users/user-login/passrecovery'));
        // ...

        // РЕСУРСЫ АДМИНА !
		$this->addRes('admin/comments/allcomments');
		$this->addRes('admin/index');
		$this->addRes('admin/login/login');
		$this->addRes('admin/login/success');
		$this->addRes('admin/login/logout');
		$this->addRes('admin/item/index');
		$this->addRes('admin/index/index');
		$this->addRes('admin/item/create');
		$this->addRes('admin/item/success');
		$this->addRes('admin/item/update');
		$this->addRes('admin/admin.category/index');
		$this->addRes('admin/comments/index');
		$this->addRes('admin/comments/edit');
		$this->addRes('admin/comments/delete');
		$this->addRes('admin/comments/success');
        $this->addRes('admin/users/index');
        $this->addRes('admin/index/information');
        $this->addRes('admin/index/settings');
        //...

        //Выставляем права, по-умолчанию всё запрещено
        $this->deny(null, null, null);

		//Для Гостя
		$this->guest('users/index/index');
		$this->guest('users/user.login/login');
		$this->guest('catalog/menu/main');
        $this->guest('catalog/index/index');
		$this->guest('catalog/menu/contact');
		$this->guest('catalog/item/display');
		$this->guest('catalog/item/success');
		$this->guest('catalog/index/search');
        $this->guest('catalog/menu/tests');
		$this->guest('admin/login/login');
		$this->guest('registration/index/index');
		$this->guest('registration/index/validate');
        $this->guest('users/user-login/passrecovery');
		$this->denyGuest('users/user-login/userprofile');
		$this->denyGuest('users/user-login/editprofile');
		$this->denyGuest('admin/index/information');

		//Для Пользователя
        $this->user('index/index');
        $this->user('users/user-login/userprofile');
        $this->user('users/user-login/editprofile');
        $this->user('catalog/item/display');
        $this->denyUser('users/user.login/login');
		$this->denyUser('users/index/index');
        $this->denyUser('admin/item/index');
        $this->denyUser('admin/index/information');
        $this->denyUser('users/user-login/passrecovery');

		//Для администратоа
        $this->admin('admin/index');
		$this->admin('admin/login/login');
		$this->admin('admin/login/success');
		$this->admin('admin/login/logout');
		$this->admin('admin/item/index');
		$this->admin('admin/item/create');
		$this->admin('admin/item/success');
		$this->admin('admin/item/update');
		$this->admin('admin/admin.category/index');
		$this->admin('admin/comments/allcomments');
		$this->admin('admin/comments/index');
		$this->admin('admin/comments/edit');
		$this->admin('admin/comments/delete');
		$this->admin('admin/comments/success');
		$this->admin('admin/index/index');
		$this->admin('admin/index/information');
        $this->admin('admin/users/index');
        $this->admin('admin/index/settings');
        //Для demo_admin

       //$this->denyDemoAdmin('admin/users/index');
      // $this->denyDemoAdmin('admin/index/information');
       $this->denyDemoAdmin('admin/index/settings');
    }
public function addRes($data){
	return $res=$this->add(new Zend_Acl_Resource($data));
}

public function denyGuest($res)
{
return $deny=$this->deny('guest',$res);
}

	public function guest($res)
	{
	return $allow=$this->allow('guest',$res);
	}

public function user($res)
{
return $allow=$this->allow('user',$res);
}
public function denyUser($res)
{
return $deny=$this->deny('user',$res);
}

public function admin($res){
	return $allow=$this->allow('admin',$res);
}
public function demoAdmin($res){
    return $allow=$this->allow('demo_admin',$res);
}
public function denyDemoAdmin($res){
    return $deny = $this->deny('demo_admin',$res);
}

public function can(){
	$request = Zend_Controller_Front::getInstance()->getRequest();

	$resource=$request->getModuleName().'/'.$request->getControllerName().'/'.$request->getActionName();

		if($this->has($resource))
		{
		$role=Zend_Auth::getInstance()->getStorage()->read();
		//echo $this->isAllowed($role->status, $resource) ? "разрешен" : "запрещен";
			if($this->isAllowed($role->status,$resource)){
				return $this->isAllowed($role->status,$resource); 
			}
			else{
					return false;
				 }
		}
		else{
			return true;
			}
	}
}
